package com.rongke.web.api;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.rongke.annotation.SourceAuthority;
import com.rongke.commons.JsonResp;
import com.rongke.enums.SourceType;
import com.rongke.model.User;
import com.rongke.model.UserAuth;
import com.rongke.service.UserAuthService;
import com.rongke.service.UserService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @UserAuthController
 * @Controller
 * @version : Ver 1.0
 */
@RestController
@RequestMapping(value="/api/userAuth")
@Transactional
@CrossOrigin
public class UserAuthController {
    private Logger log = Logger.getLogger(this.getClass());
    @Autowired
    private UserAuthService userAuthService;
    @Autowired
    private UserService userService;

    /**
     * @添加
     * @param userAuth
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/add", method = RequestMethod.POST)
    public JsonResp addUserAuth(@RequestBody UserAuth userAuth){
        log.debug("添加");
        userAuthService.insert(userAuth);
        return JsonResp.ok(userAuth);
    }

    /**
     * @修改
     * @param userAuth
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/update", method = RequestMethod.POST)
    public JsonResp updateUserAuth(@RequestBody UserAuth userAuth){
        log.debug("修改");
        userAuthService.updateById(userAuth);
        return JsonResp.ok(userAuth);
    }
    /**
     * @根据id查找
     * @param id
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/selectOne", method = RequestMethod.GET)
    public JsonResp selectUserAuth(Long id){
        log.debug("查找");
        UserAuth userAuth = userAuthService.selectById(id);
        return JsonResp.ok(userAuth);
    }

    /**
     * @认证表
     * @param
     * @return 返回值JsonResp
     */
    @SourceAuthority(SourceType.APP)
    @RequestMapping(value="/selectAll", method = RequestMethod.GET)
    public JsonResp selectAll(){
        log.debug("认证表");
        User user = userService.findLoginUser();
        EntityWrapper<UserAuth> ew = new EntityWrapper<>();
        ew.eq("user_id",user.getId());
        UserAuth userAuth = userAuthService.selectOne(ew);
        Map map = new HashMap();
        map.put("socre",user.getAuthScore());
        map.put("auth",userAuth);
        return JsonResp.ok(map);
    }


}
